package org.jeecg.modules.demo.yxqcyfOrder.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.jeecg.modules.demo.utils.BigDecimalUtil;
import org.jeecg.modules.demo.utils.ReflectUtil;
import org.jeecg.modules.demo.yxqcyfOrder.entity.PayableGeneralLedgerVo;
import org.jeecg.modules.demo.yxqcyfOrder.entity.PayableSubLedgerVo;
import org.jeecg.modules.demo.yxqcyfOrder.entity.YxQcyfOrder;
import org.jeecg.modules.demo.yxqcyfOrder.mapper.YxQcyfOrderMapper;
import org.jeecg.modules.demo.yxqcyfOrder.service.IYxQcyfOrderService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description: 期初应付款单
 * @Author: jeecg-boot
 * @Date:   2023-03-15
 * @Version: V1.0
 */
@Service
public class YxQcyfOrderServiceImpl extends ServiceImpl<YxQcyfOrderMapper, YxQcyfOrder> implements IYxQcyfOrderService {

    @Resource
    private YxQcyfOrderMapper yxQcyfOrderMapper;


    @Override
    public String getMaxCode(){
        return yxQcyfOrderMapper.getMaxCode();
    }

    @Override
    public List<YxQcyfOrder> getListByPoOrder(String poOrder) {
        return yxQcyfOrderMapper.getListByPoOrder(poOrder);
    }

    @Override
    public List<PayableGeneralLedgerVo> getPayadbleGeneralLedgerList(){
        List<PayableGeneralLedgerVo> resultList =  Collections.synchronizedList(yxQcyfOrderMapper.getMeetList());
        for (PayableGeneralLedgerVo payableGeneralLedgerVo : resultList) {
            Double totalPaid = yxQcyfOrderMapper.getTotalPaid(payableGeneralLedgerVo.getChumdwmc());
            Double advance = yxQcyfOrderMapper.getTotalAdvance(payableGeneralLedgerVo.getChumdwmc());
            payableGeneralLedgerVo.setPaid(BigDecimalUtil.add(totalPaid,advance));
            payableGeneralLedgerVo.setOpningBalance(yxQcyfOrderMapper.getOpeningBalance(payableGeneralLedgerVo.getChumdwmc()));
        }
        return resultList;

    }


    @Override
    public List<PayableGeneralLedgerVo> getReceivableList(){
        List<PayableGeneralLedgerVo> resultList =  Collections.synchronizedList(yxQcyfOrderMapper.getReceivableList());
        for (PayableGeneralLedgerVo payableGeneralLedgerVo : resultList) {
            Double totalPaid = yxQcyfOrderMapper.getTotalReceived(payableGeneralLedgerVo.getChumdwmc());
            Double advance = yxQcyfOrderMapper.getTotalCollect(payableGeneralLedgerVo.getChumdwmc());
            payableGeneralLedgerVo.setPaid(BigDecimalUtil.add(totalPaid,advance));
            payableGeneralLedgerVo.setOpningBalance(yxQcyfOrderMapper.getSaleOpeningBalance(payableGeneralLedgerVo.getChumdwmc()));
        }
        return resultList;

    }



    @Override
    public List<PayableSubLedgerVo> getMeetDetailList(String suplier){
        List<PayableSubLedgerVo> resultList = new CopyOnWriteArrayList<>();
        PayableSubLedgerVo qichu = yxQcyfOrderMapper.getOpeningList(suplier);
        List<PayableSubLedgerVo> yufuList = yxQcyfOrderMapper.getPaymentInAdvanceList(suplier);
        if(ReflectUtil.isObjectNull(qichu) || qichu == null){
            singleThreadExecutor(resultList,getVo(suplier,1));
            if(yufuList.size()!=0){
                for (PayableSubLedgerVo subLedgerVo : yufuList) {
                    singleThreadExecutor(resultList,subLedgerVo);
                }
            }
        }else{
            singleThreadExecutor(resultList,qichu);
            if(yufuList.size()!=0){
                for (PayableSubLedgerVo subLedgerVo : yufuList) {
                    singleThreadExecutor(resultList,subLedgerVo);
                }
            }
            List<PayableSubLedgerVo> meetSopayOrderList = yxQcyfOrderMapper.getOpeningSopayOrderList(qichu.getDocumentNumber());
            for (PayableSubLedgerVo subLedgerVo : meetSopayOrderList) {
                singleThreadExecutor(resultList,subLedgerVo);
                if(subLedgerVo.getBusinessType().equals("预付冲应付")){
                    singleThreadExecutor(resultList,getRedVo(subLedgerVo));
                }
            }

        }
        List<PayableSubLedgerVo> invoiceList = yxQcyfOrderMapper.getPuinvoice(suplier);
        if(invoiceList.size()!=0){
            for (PayableSubLedgerVo payableSubLedgerVo : invoiceList) {
                singleThreadExecutor(resultList,payableSubLedgerVo);
                List<PayableSubLedgerVo> sopayOrderList = yxQcyfOrderMapper.getSopayOrder(payableSubLedgerVo.getDocumentNumber());
                if (sopayOrderList.size()!=0){
                    for (PayableSubLedgerVo subLedgerVo : sopayOrderList) {
                        singleThreadExecutor(resultList,subLedgerVo);
                        if(subLedgerVo.getBusinessType().equals("预付冲应付")){
                            singleThreadExecutor(resultList,getRedVo(subLedgerVo));
                        }
                    }
                }
            }
        }
        return resultList;

    }

    @Override
    public List<PayableSubLedgerVo> getSaleReceivableDetailData(String suplier){
        List<PayableSubLedgerVo> resultList = new CopyOnWriteArrayList<>();
        PayableSubLedgerVo qichu = yxQcyfOrderMapper.getSaleOpeningList(suplier);
        List<PayableSubLedgerVo> yushouList = yxQcyfOrderMapper.getSalePaymentInAdvanceList(suplier);
        if(ReflectUtil.isObjectNull(qichu)|| qichu == null){
            singleThreadExecutor(resultList,getVo(suplier,1));
            if(yushouList.size()!=0){
                for (PayableSubLedgerVo payableSubLedgerVo : yushouList) {
                    singleThreadExecutor(resultList,payableSubLedgerVo);
                }
            }
        }else{
            singleThreadExecutor(resultList,qichu);
            if(yushouList.size()!=0){
                for (PayableSubLedgerVo payableSubLedgerVo : yushouList) {
                    singleThreadExecutor(resultList,payableSubLedgerVo);
                }
            }
            List<PayableSubLedgerVo> meetSopayOrderList = yxQcyfOrderMapper.getSaleOpeningSopayOrderList(qichu.getDocumentNumber());
            for (PayableSubLedgerVo subLedgerVo : meetSopayOrderList) {
                singleThreadExecutor(resultList,subLedgerVo);
                if(subLedgerVo.getBusinessType().equals("预收冲应收")){
                    singleThreadExecutor(resultList,getRedVo(subLedgerVo));
                }
            }
        }

        List<PayableSubLedgerVo> invoiceList = yxQcyfOrderMapper.getSoinvoice(suplier);
        if(invoiceList.size()!=0){
            for (PayableSubLedgerVo payableSubLedgerVo : invoiceList) {
                singleThreadExecutor(resultList,payableSubLedgerVo);
                List<PayableSubLedgerVo> sopayOrderList = yxQcyfOrderMapper.getSaleListBySoOrder(payableSubLedgerVo.getDocumentNumber());
                if (sopayOrderList.size()!=0){
                    for (PayableSubLedgerVo subLedgerVo : sopayOrderList) {
                        singleThreadExecutor(resultList,subLedgerVo);
                        if(subLedgerVo.getBusinessType().equals("预收冲应收")){
                            singleThreadExecutor(resultList,getRedVo(subLedgerVo));
                        }
                    }
                }
            }
        }
        return resultList;
    }



    public synchronized static List<PayableSubLedgerVo> singleThreadExecutor(List<PayableSubLedgerVo> resultList,PayableSubLedgerVo payableSubLedgerVo) {
        ExecutorService threadPool = Executors.newSingleThreadExecutor();
        Thread t1 = new Thread(() -> resultList.add(payableSubLedgerVo));
        threadPool.execute(t1);
        threadPool.shutdown();
        return resultList;
    }

    public PayableSubLedgerVo getVo(String suplier,int type){
        PayableSubLedgerVo payableSubLedgerVo = new PayableSubLedgerVo();
        payableSubLedgerVo.setChumdwmc(suplier);
        payableSubLedgerVo.setCreateTime(null);
        payableSubLedgerVo.setDocumentType(type==1?"期初余额":type==2?"采购发票":type==3?"销售发票":type==4?"收款单":type==5?"付款单":"其他");
        payableSubLedgerVo.setMeet(0.00);
        payableSubLedgerVo.setPaid(0.00);
        payableSubLedgerVo.setBalance(0.00);
        payableSubLedgerVo.setMark(1);
        return payableSubLedgerVo;
    }
    public PayableSubLedgerVo getRedVo(PayableSubLedgerVo payableSubLedgerVo){
        PayableSubLedgerVo vo = new PayableSubLedgerVo();
        vo.setChumdwmc(payableSubLedgerVo.getChumdwmc());
        vo.setPoOrder(payableSubLedgerVo.getPoOrder());
        vo.setCreateTime(payableSubLedgerVo.getCreateTime());
        vo.setDocumentType(payableSubLedgerVo.getDocumentType());
        vo.setDocumentNumber(payableSubLedgerVo.getDocumentNumber());
        vo.setBusinessType(payableSubLedgerVo.getBusinessType());
        vo.setMeet(payableSubLedgerVo.getMeet());
        vo.setPaid(payableSubLedgerVo.getPaid()*-1);
        vo.setBalance(payableSubLedgerVo.getBalance());
        vo.setWoa(payableSubLedgerVo.getWoa());
        vo.setMark(1);
        return vo;
    }
}
